package com.zpyr.mvc.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import org.mybatis.spring.SqlSessionTemplate;

import com.zpyr.common.StringUtil;
import com.zpyr.mvc.vo.Carc_evaluation;
import com.zpyr.mvc.vo.Carc_info;

import com.zpyr.mvc.interfaces.CarcMapper;

@Controller
public class CarcMobCont {
	
	@Autowired
	private SqlSessionTemplate sqlSession;
	
	@RequestMapping(value="/carcenter.gcm" , params={"!nouse"})
	public ModelAndView index(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		ModelAndView mav = new ModelAndView("carc/m/index");
		return mav;
	}
	
	
	/**
	 * 카센터 정보 보기
	 * @param info_seq
	 */
	@RequestMapping(value="/carc/m/v/carc_info.gcm" , params={"!nouse"})
	public ModelAndView viewCarc_info(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		Carc_info carc_info = mapper.getCarc_info(info_seq);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("info_seq" , info_seq);
		List<Carc_evaluation> eval_list = mapper.getList_carc_evaluation(parameters);
		
		ModelAndView mav = new ModelAndView("carc/m/carc_info");
		
		mav.addObject("carc_info", carc_info);
		mav.addObject("eval_list", eval_list);
		return mav;
	}

	/**
	 * 카센터 정보 지도 보기
	 * @param info_seq
	 */
	@RequestMapping(value="/carc/m/v/carc_info_map.gcm" , params={"!nouse"})
	public ModelAndView viewCarc_info_map(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		Carc_info carc_info = mapper.getCarc_info(info_seq);
		ModelAndView mav = new ModelAndView("carc/m/carc_info_map");
		mav.addObject("carc_info", carc_info);
		return mav;
	}

	/**
	 * 카센터 평가페이지 보기
	 * @param info_seq
	 */
	@RequestMapping(value="/carc/m/v/carc_info_evaluation.gcm" , params={"!nouse"})
	public ModelAndView viewCarc_info_evaluation(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		Carc_info carc_info = mapper.getCarc_info(info_seq);
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("info_seq" , info_seq);
		List<Carc_evaluation> eval_list = mapper.getList_carc_evaluation(parameters);
		
		ModelAndView mav = new ModelAndView("carc/m/carc_info_evaluation");
		mav.addObject("carc_info", carc_info);
		mav.addObject("eval_list", eval_list);
		return mav;
	}
	
	/**
	 * 카센터 평가 입력.
	 * @param info_seq
	 * @param writer_name
	 * @param grade
	 * @param content
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/carc/m/i/evaluation.gcm" , params={"!nouse"})
	public ModelAndView doInsertEvaluation(String info_seq , String writer_name , String grade , String content) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("info_seq", info_seq);
		parameters.put("writer_name", writer_name);
		parameters.put("grade", grade);
		parameters.put("content", content);
		int ret = mapper.doInsertEvaluation( parameters );
		parameters.put("grade_total_point", grade);
		mapper.updateGrade(parameters);
		ModelAndView mav = new ModelAndView("carc/m/isSuccess");
		mav.addObject("retCode" , ret);
		return mav;
	}

	@RequestMapping(value="/carc/m/v/search_around.gcm" , params={"!nouse"})
	public ModelAndView searchAroundByDistance(String distance , String lat , String lng) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("distance", distance);
		parameters.put("lat", lat);
		parameters.put("lng", lng);
		
		List<Carc_info> retList = mapper.searchAroundByDistance(parameters);
		
		ModelAndView mav = new ModelAndView("carc/m/search_around");
		mav.addObject("carc_list" , retList);
		return mav;		
	}

	@RequestMapping(value="/carc/m/v/search_around_map.gcm" , params={"!nouse"})
	public ModelAndView searchAroundMapByDistance(String distance , String lat , String lng) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("distance", distance);
		parameters.put("lat", lat);
		parameters.put("lng", lng);
		
		List<Carc_info> retList = mapper.searchAroundByDistance(parameters);
		
		ModelAndView mav = new ModelAndView("carc/m/search_around_map");
		mav.addObject("carc_list" , retList);
		return mav;		
	}
	
	@RequestMapping(value="/carc/m/v/search_name.gcm" , params={"!nouse"})
	public ModelAndView searchName(String search_opt , String search_word) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("search_opt", search_opt);
		parameters.put("search_word", search_word);
		
		List<Carc_info> retList = null;
		if(search_opt != null && !search_opt.equals("") && search_word != null && !search_word.equals("") ) {
			retList = mapper.searchName(parameters);
		}
		
		ModelAndView mav = new ModelAndView("carc/m/search_name");
		mav.addObject("carc_list" , retList);
		return mav;		
	}

	@RequestMapping(value="/carc/m/v/search_name_map.gcm" , params={"!nouse"})
	public ModelAndView searchName_map(String search_opt , String search_word) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("search_opt", search_opt);
		parameters.put("search_word", search_word);
		
		List<Carc_info> retList = null;
		if(search_opt != null && !search_opt.equals("") && search_word != null && !search_word.equals("") ) {
			retList = mapper.searchName(parameters);
		}
		
		ModelAndView mav = new ModelAndView("carc/m/search_name_map");
		mav.addObject("carc_list" , retList);
		return mav;		
	}
	
	@RequestMapping(value="/carc/m/xml/sido_list.gcm" , params={"!nouse"})
	public ModelAndView selectSiDoList() {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		List<String> retList = null;
		retList = mapper.selectSiDoList();
		
		ModelAndView mav = new ModelAndView("carc/m/xml/singleList");
		mav.addObject("retList" , retList);
		return mav;
	}

	@RequestMapping(value="/carc/m/xml/gubun_list.gcm" , params={"!nouse"})
	public ModelAndView selectGuBunList( String addr_sido ) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		List<String> retList = null;
		retList = mapper.selectGuBunList(addr_sido);
		
		ModelAndView mav = new ModelAndView("carc/m/xml/singleList");
		mav.addObject("retList" , retList);
		return mav;
	}
	
	@RequestMapping(value="/carc/m/xml/dong_list.gcm" , params={"!nouse"})
	public ModelAndView selectDongList( String addr_sido , String addr_gubun ) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		List<String> retList = null;
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("addr_sido", addr_sido);
		parameters.put("addr_gubun", addr_gubun);
		retList = mapper.selectDongList(parameters);
		
		ModelAndView mav = new ModelAndView("carc/m/xml/singleList");
		mav.addObject("retList" , retList);
		return mav;
	}

	@RequestMapping(value="/carc/m/v/search_list.gcm" , params={"!nouse"})
	public ModelAndView searchList() {
		ModelAndView mav = new ModelAndView("carc/m/search_list");
		return mav;
	}

	@RequestMapping(value="/carc/m/v/search_list_result.gcm" , params={"!nouse"})
	public ModelAndView searchList( String addr_sido , String addr_gubun , String addr_dong ) {
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		
		List<Carc_info> retList = null;
		HashMap<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("addr_sido", addr_sido);
		parameters.put("addr_gubun", addr_gubun);
		parameters.put("addr_dong", addr_dong);
		retList = mapper.searchList(parameters);
		
		ModelAndView mav = new ModelAndView("carc/m/search_list_result");
		mav.addObject("retList" , retList);
		return mav;
	}

	/**
	 * 즐겨찾기 페이지 이동
	 * @param info_seq
	 * @return
	 */
	@RequestMapping(value="/carc/m/v/favorites.gcm" , params={"!nouse"})
	public ModelAndView favorites(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		ModelAndView mav = new ModelAndView("carc/m/favorites");
		return mav;
	}
	
	
	/////////////////////  센차터치 적용     /////////////////////
	@RequestMapping(value="/carc.gcm" , params={"!nouse"})
	public ModelAndView m2_index(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		ModelAndView mav = new ModelAndView("carc/m2/index");
		return mav;
	}

	@RequestMapping(value="/carcenter/view/carc_info.gcm" , params={"!nouse"})
	public ModelAndView sencha(/*@RequestParam("info_seq")*/ String info_seq) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		
		ModelAndView mav = new ModelAndView("carc/m2/carc_info");
		return mav;
	}

	@RequestMapping(value="/carcenter/insert/carc_user_info.gcm" , params={"!nouse"})
	public ModelAndView insertCarcUserInfo(/*@RequestParam("info_seq")*/ String name , String addr , String category1 , String category2 , String description ) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		HashMap<String , Object> parameters = new HashMap<String, Object>();
		parameters.put("name", name);
		parameters.put("addr", addr);
		parameters.put("category1", category1);
		parameters.put("category2", category2);
		parameters.put("description", description);
		
		int retCnt = mapper.insertCarcUserInfo(parameters);
		
		ModelAndView mav = new ModelAndView("carc/common/isSucYN");
		mav.addObject("isSuccess" , retCnt >=1 ? "Y" : "N" );
		return mav;
	}
	
	@RequestMapping(value="/carcenter/insert/carc_member.gcm" , params={"!nouse"})
	public ModelAndView insertCarcMember(/*@RequestParam("info_seq")*/ String login_email , String mem_nickname , String password ) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		CarcMapper mapper = sqlSession.getMapper(CarcMapper.class);
		HashMap<String , Object> parameters = new HashMap<String, Object>();
		
		parameters.put("mem_nickname", mem_nickname);
		String errMsg = "N";
		if( mapper.selectMember(parameters) != null ) {
			errMsg = "duplicate_nickname";
		}
		parameters.clear();

		parameters.put("login_email", login_email);
		if( mapper.selectMember(parameters) != null ) {
			errMsg = "duplicate_email";
		}
		parameters.clear();
		
		
		parameters.put("mem_nickname", mem_nickname);
		parameters.put("login_email", login_email);
		parameters.put("mem_name", mem_nickname);
		parameters.put("password", password == null ? "" : StringUtil.encodeMD5(password ) );
		
		int retCnt = 0;
		if(errMsg.equals("N")) retCnt = mapper.insertMember(parameters); 
		
		ModelAndView mav = new ModelAndView("carc/common/isSucYN");
		mav.addObject("isSuccess" , retCnt >=1 ? "Y" : errMsg );
		return mav;
	}
		
}